package gui.aula;


import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.border.TitledBorder;
import javax.swing.table.TableRowSorter;

import modelo.ModeloAula;

public class PanelAula extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel panelTableAula = null;
	private JScrollPane jspAula = null;
	private JTable tblAula = null;
	private JButton btnAgregar = null;
	private JButton btnModificar = null;
	private JPanel panelBuscar = null;
	private JLabel lblBuscar = null;
	private JComboBox cmbBuscar = null;
	private JTextField txtBuscar = null;
	private ModeloAula modeloAula = null;
	private int indiceComboBuscar = 0;
	private static int idAula = 0;
	private static String nombreAula = null;

	/**
	 * This is the default constructor
	 */
	public PanelAula() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		modeloAula = ModeloAula.getInstancia();
		this.setSize(884, 378);
		this.setLayout(null);
		this.add(getPanelTableAula(), null);
		this.add(getBtnAgregar(), null);
		this.add(getBtnModificar(), null);
		this.add(getPanelBuscar(), null);
	}

	/**
	 * This method initializes panelTableCiclo
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelTableAula() {
		if (panelTableAula == null) {
			panelTableAula = new JPanel();
			panelTableAula.setLayout(null);
			panelTableAula.setBounds(new Rectangle(6, 90, 871, 227));
			panelTableAula.setBorder(BorderFactory.createTitledBorder(
					BorderFactory.createLineBorder(Color.gray, 1),
					"Lista Aulas", TitledBorder.LEFT,
					TitledBorder.DEFAULT_POSITION, new Font("Dialog",
							Font.BOLD, 14), new Color(51, 51, 51)));
			panelTableAula.add(getJspAula(), null);
		}
		return panelTableAula;
	}

	/**
	 * This method initializes jspCiclo
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJspAula() {
		if (jspAula == null) {
			jspAula = new JScrollPane();
			jspAula.setBounds(new Rectangle(12, 25, 850, 185));
			jspAula.setViewportView(getTblAula());
		}
		return jspAula;
	}

	/**
	 * This method initializes tblCiclo
	 * 
	 * @return javax.swing.JTable
	 */
	private JTable getTblAula() {
		if (tblAula == null) {
			tblAula = new JTable(modeloAula);
		}
		return tblAula;
	}

	/**
	 * This method initializes btnAgregar
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnAgregar() {
		if (btnAgregar == null) {
			btnAgregar = new JButton();
			btnAgregar.setBounds(new Rectangle(174, 331, 108, 33));
			btnAgregar.setText("Agregar");
			btnAgregar.setIcon(new ImageIcon("./lib/images/add.png"));
			btnAgregar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					DialogoAgregarAula dialogo = new DialogoAgregarAula(null);
					dialogo.setVisible(true);
				}
			});
		}
		return btnAgregar;
	}

	/**
	 * This method initializes btnModificar
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnModificar() {
		if (btnModificar == null) {
			btnModificar = new JButton();
			btnModificar.setBounds(new Rectangle(293, 331, 108, 33));
			btnModificar.setText("Modificar");
			btnModificar.setIcon(new ImageIcon("./lib/images/modify.png"));
			btnModificar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (tblAula.isRowSelected(tblAula.getSelectedRow()) == false) {
						JOptionPane
								.showMessageDialog(null,
										"Debe de Seleccionar un Aula para Modificarlo");
					} else {
						int id = Integer.parseInt(tblAula.getValueAt(
								tblAula.getSelectedRow(), 0).toString());
						PanelModificarAula panelModificarAula = PanelModificarAula.getInstancia();
						panelModificarAula.setIdModificar(id);
						panelModificarAula.llenarCampos();
						DialogoModificarAula.getInstancia().setVisible(true);
					}
				}
			});
		}
		return btnModificar;
	}

	/**
	 * This method initializes btnEliminar
	 * 
	 * @return javax.swing.JButton
	 */
		/**
	 * This method initializes panelBuscar
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelBuscar() {
		if (panelBuscar == null) {
			lblBuscar = new JLabel();
			lblBuscar.setBounds(new Rectangle(10, 21, 55, 23));
			lblBuscar.setText("Buscar:");
			panelBuscar = new JPanel();
			panelBuscar.setLayout(null);
			panelBuscar.setBounds(new Rectangle(255, 18, 445, 58));
			panelBuscar.setBorder(BorderFactory.createTitledBorder(
					BorderFactory.createLineBorder(Color.black, 1), "Busqueda",
					TitledBorder.DEFAULT_JUSTIFICATION,
					TitledBorder.DEFAULT_POSITION, new Font("SansSerif",
							Font.PLAIN, 12), Color.black));
			panelBuscar.add(lblBuscar, null);
			panelBuscar.add(getCmbBuscar(), null);
			panelBuscar.add(getTxtBuscar(), null);
		}
		return panelBuscar;
	}

	/**
	 * This method initializes cmbBuscar
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getCmbBuscar() {
		if (cmbBuscar == null) {
			cmbBuscar = new JComboBox();
			cmbBuscar.setBounds(new Rectangle(67, 21, 151, 23));
			cmbBuscar.setModel(new DefaultComboBoxModel(new String[] {
					"ID Aula", "Nombre", "Empleado", "Aula" }));
			cmbBuscar.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					indiceComboBuscar = cmbBuscar.getSelectedIndex();
				}
			});
		}
		return cmbBuscar;
	}

	/**
	 * This method initializes txtBuscar
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtBuscar() {
		if (txtBuscar == null) {
			txtBuscar = new JTextField();
			txtBuscar.setBounds(new Rectangle(221, 21, 210, 23));
			txtBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyReleased(java.awt.event.KeyEvent e) {
					if (e.getKeyCode() == KeyEvent.VK_BACK_SLASH) {
						getTxtBuscar().setText("");
					}
					TableRowSorter<ModeloAula> sorter = new TableRowSorter<ModeloAula>(
							modeloAula);
					tblAula.setRowSorter(sorter);
					sorter.setRowFilter(RowFilter.regexFilter("(?i)"
							.concat(getTxtBuscar().getText()),
							indiceComboBuscar));
				}
			});

		}
		return txtBuscar;
	}

	/**
	 * This method initializes checkBoxActivo
	 * 
	 * @return javax.swing.JCheckBox
	 */
	
	
	
	/**
	 * This method initializes btnVerListaEstudiante
	 * 
	 * @return javax.swing.JButton
	 */
	public static int getIdAula() {
		return idAula;
	}

	public static void setIdAula(int idAula) {
		PanelAula.idAula = idAula;
	}

	public static String getNombreAula() {
		return nombreAula;
	}

	public static void setNombreAula(String nombreAula) {
		PanelAula.nombreAula = nombreAula;
	}
}
